package com.qq.first_project.controller.page;

import com.qq.first_project.common.R;
import com.qq.first_project.entity.Type;
import com.qq.first_project.entity.vo.FirstPageBlogVo;
import com.qq.first_project.service.BlogService;
import com.qq.first_project.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin //跨域
@RequestMapping("/page/type")
public class TypeBlogShowController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    //查询所有分类及各分类下的博客列表
    @GetMapping("/list")
    R getTypes(){
        List<Type> types = typeService.getAllTypeAndBlog();
        return R.ok().data("data",types).message("获取成功");
    }

    //根据分类ID查询该分类下的博客列表
    @GetMapping("/blogs")
    R getBlogsById(ModelMap modelMap, @RequestParam Long id){
        List<Type> types = typeService.getAllTypeAndBlog();

        //id为-1表示从首页导航栏点击进入分类页面
        if (id == -1){
            id = types.get(0).getId();
        }
        List<FirstPageBlogVo> blogs = blogService.getByTypeId(id);

        modelMap.addAttribute("blogs",blogs);
        modelMap.addAttribute("activeTypeId",id);

        return R.ok().data("data",modelMap);
    }
}
